package adapter;

import Util.InputFilterMinMax;
import Util.TConfig;
import Util.Util;
import android.app.Activity;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.thanhnx.tlg1.MyApp;
import com.thanhnx.tlg1.R;

public class ColAAdapter extends BaseAdapter {
	private int item_h_100;

	private int item_h;
	private int item_h_unit;// 5 dips
	private int MAX;
	private int MIN;

	private int cur_forcus = 0;
	private MyApp myApp;
	private final Activity context;
	private int[] list;

	public ColAAdapter(Activity context, int[] list) {
		this.context = context;
		this.list = list;
		myApp = (MyApp) context.getApplicationContext();
		this.item_h = Util.convertDpToPixel(20, context);
		this.item_h_unit = Util.convertDpToPixel(5, context);
		this.MIN = 3 * this.item_h_unit;// 15dip
		this.MAX = 16 * this.item_h_unit;// 80dip
		this.item_h_100 = this.item_h;
	}

	public void requestZoom(int progress) {
		float x = (float) (progress / 100.0);
		item_h = (int) (x * item_h_100);
		Log.e("requestZoom(int progress)", "progress = " + progress
				+ " item_h = " + item_h);
		notifyDataSetChanged();
	}

	public void requestBiggerRowX(int x) {
		item_h = (int) (item_h + TConfig.zoom_ratio * x * item_h);
		notifyDataSetChanged();
	}

	public void requestSmallerRowX(int x) {
		item_h = (int) (item_h - TConfig.zoom_ratio * x * item_h);
		notifyDataSetChanged();
	}

	public void requestBiggerRow() {
		if (item_h + TConfig.zoom_ratio * item_h < MAX) {
			item_h = (int) (item_h + TConfig.zoom_ratio * item_h);
			notifyDataSetChanged();
		}
	}

	public void requestSmallerRow() {
		if (item_h - TConfig.zoom_ratio * item_h > MIN) {
			item_h = (int) (item_h - TConfig.zoom_ratio * item_h);
			notifyDataSetChanged();
		}
	}

	public void requestBiggerRowPlus() {
		if (item_h + TConfig.zoom_ratio_plus * item_h < MAX) {
			item_h = (int) (item_h + TConfig.zoom_ratio_plus * item_h);
			notifyDataSetChanged();
		}
	}

	public void requestSmallerRowPlus() {
		if (item_h - TConfig.zoom_ratio_plus * item_h > MIN) {
			item_h = (int) (item_h - TConfig.zoom_ratio_plus * item_h);
			notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		return list.length;
	}

	@Override
	public Object getItem(int arg0) {
		return list[arg0];
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	static class ViewHolder {
		protected LinearLayout ll_root;
		protected EditText et;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parrent) {
		Log.e("ColAAdapter: get view at " + position, " = " + list[position]);
		View view = null;
		if (convertView == null) {
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.item_col_a, null);
			final ViewHolder viewholder = new ViewHolder();
			viewholder.ll_root = (LinearLayout) view
					.findViewById(R.id.ll_root_col_a);
			viewholder.ll_root.setLayoutParams(new AbsListView.LayoutParams(
					LayoutParams.MATCH_PARENT, item_h));
			viewholder.et = (EditText) view.findViewById(R.id.item_col_a);
			viewholder.et.setOnFocusChangeListener(new OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					if (!hasFocus) {
						int real_position = (Integer) viewholder.et.getTag();
						String str = viewholder.et.getText().toString();
						if (!TextUtils.isEmpty(str)) {
							if ((real_position - 1) >= 0
									&& (list[real_position - 1] == -1)) {
								viewholder.et.setText("");
								Toast.makeText(context,
										"You have to fill previous blank cell",
										Toast.LENGTH_SHORT).show();
							} else {
								list[real_position] = Integer.parseInt(str);
								myApp.updateAt(real_position);
								Log.e("focus change at position = "
										+ real_position, "value =  " + str);
								cur_forcus = real_position + 1;
							}
						} else {
						}
					} else {
						int real_position = (Integer) viewholder.et.getTag();
						Log.e("Start focus ", "start at " + real_position);
					}
				}
			});
			viewholder.et.setFilters(new InputFilter[] { new InputFilterMinMax(
					0, 36) });
			view.setTag(viewholder);
			viewholder.et.setTag(position);
		} else {
			view = convertView;
			((ViewHolder) view.getTag()).et.setTag(position);
		}
		final ViewHolder holder = (ViewHolder) view.getTag();
		holder.ll_root.setLayoutParams(new AbsListView.LayoutParams(
				LayoutParams.MATCH_PARENT, item_h));
//		int value = list[position];
//		if (value >= 0) {
//			holder.et.setText(value + "");
//		} else {
//			holder.et.setText("");
//		}
		holder.et.setText(position+"");
		return view;
	}
}
